﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cataloger.Okna.Podstawowe
{
    public partial class FrmEdytorKolumn : Form
    {
        public FrmEdytorKolumn()
        {
            InitializeComponent();
        }
        private bool CzyWidoczna(ColumnHeader column)
        {
            if (column.Width == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        ListView.ColumnHeaderCollection naglow;

        public ListView.ColumnHeaderCollection Naglow
        {
            get { return naglow; }
            set { naglow = value; }
        }
        public FrmEdytorKolumn(ListView.ColumnHeaderCollection Naglowki):this()
        {
            foreach (ColumnHeader col in Naglowki)
            {
                checkedListBox1.Items.Add(col.Text, CzyWidoczna(col));
                
            }
            naglow = Naglowki;
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ColumnHeader c in naglow)
            {
                if (checkedListBox1.SelectedItem.ToString()==c.Text)
                {
                    textBox1.Text=c.Width.ToString();

                }

            }
            
        }
        
        private void button2_Click(object sender, EventArgs e)
        {
            foreach (ColumnHeader c in naglow)
            {
                if (!checkedListBox1.CheckedItems.Contains(c.Text))
                {
                    c.Width = 0;

                }
             
            }
            this.DialogResult = DialogResult.OK;
     

        }

        private void button3_Click(object sender, EventArgs e)
        {
            foreach (ColumnHeader c in naglow)
            {
                if (checkedListBox1.SelectedItem.ToString() == c.Text)
                {
                    try{
                    c.Width=int.Parse(textBox1.Text);
                    }
                    catch{}

                }

            }
        }

        private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.CurrentValue == CheckState.Checked)
            {
                try
                {
                    naglow[e.Index].Width = 0;

                    textBox1.Text = naglow[e.Index].Width.ToString();
                }
                catch { }
            }
            else if (e.CurrentValue == CheckState.Unchecked)
            {
                try
                {
                    naglow[e.Index].Width = 80;

                    textBox1.Text = naglow[e.Index].Width.ToString();
                }
                catch { }
            }

              

            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked)
            {
                foreach (ColumnHeader item in this.Naglow)
                {
                    if (item.Width > 0)
                    {
                        item.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                    }
                }
            }
        }
    }
}
